Golang : Example for ECDSA(Elliptic Curve Digital Signature Algorithm) package functions
In cryptography, the Elliptic Curve Digital Signature Algorithm (ECDSA) is a variant of the Digital Signature Algorithm (DSA) which uses elliptic curve cryptography. This tutorial is just a slight variant of the previous tutorial for DSA and we will learn how to use the ECDSA functions to do :
- generate a private key
- extract the public key from the generated private key
- use the private key to sign
- use the public key to verify the signature
ecdsaexample.go
package main
import (
"crypto/ecdsa"
"crypto/elliptic"
"crypto/md5"
"crypto/rand"
"fmt"
"hash"
"io"
"math/big"
"os"
)
func main() {
pubkeyCurve := elliptic.P256() //see http://golang.org/pkg/crypto/elliptic/#P256
privatekey := new(ecdsa.PrivateKey)
privatekey, err := ecdsa.GenerateKey(pubkeyCurve, rand.Reader) // this generates a public & private key pair
if err != nil {
fmt.Println(err)
os.Exit(1)
}
var pubkey ecdsa.PublicKey
pubkey = privatekey.PublicKey
fmt.Println("Private Key :")
fmt.Printf("%x \n", privatekey)
fmt.Println("Public Key :")
fmt.Printf("%x \n", pubkey)
// Sign ecdsa style
var h hash.Hash
h = md5.New()
r := big.NewInt(0)
s := big.NewInt(0)
io.WriteString(h, "This is a message to be signed and verified by ECDSA!")
signhash := h.Sum(nil)
r, s, serr := ecdsa.Sign(rand.Reader, privatekey, signhash)
if serr != nil {
fmt.Println(err)
os.Exit(1)
}
signature := r.Bytes()
signature = append(signature, s.Bytes()...)
fmt.Printf("Signature : %x\n", signature)
// Verify
verifystatus := ecdsa.Verify(&pubkey, signhash, r, s)
fmt.Println(verifystatus) // should be true
}
Executing the code will produce the following output( private/public keys will be different for each execution) :
go run ecdsaexample.go
Private Key :
&{{{20821a420} 127e668421cbbf6a80692679560c618d5f06281b02a8323157816e4c7ce50e2b
3b776e2f6d9febc03d3abdf91c16d15396dc6f72bec3e259df2bfdec8fe41f89}
44d0f3819c4153a23f42263034d450c3038a305038285a04f4e068b56ebe5393}
Public Key :
{{20821a420} 127e668421cbbf6a80692679560c618d5f06281b02a8323157816e4c7ce50e2b
3b776e2f6d9febc03d3abdf91c16d15396dc6f72bec3e259df2bfdec8fe41f89}
Signature :
d7b0dd08b4de09da1c70567655ba16a5437a75bdaa4917ab509bf663e71d1aeef0d6cc
11e458e66e15a4ffae70ab434eb586514e22d95e89f75ec96c48bcb4f5
true
By Adam Ng
IF you gain some knowledge or the information here solved your programming problem. Please consider donating to the less fortunate or some charities that you like. Apart from donation, planting trees, volunteering or reducing your carbon footprint will be great too.
Advertisement
Tutorials
+5.1k Golang : How to deal with configuration data?
+11.4k Get form post value in Go
+10.4k Golang : Flip coin example
+12.3k Android Studio : Highlight ImageButton when pressed on example
+7.3k Javascript : Push notifications to browser with Push.js
+9k Golang : How to get username from email address
+14.6k Golang : How to check for empty array string or string?
+6.8k Golang : Takes a plural word and makes it singular
+6.8k Golang : Squaring elements in array
+18.2k Golang : Find IP address from string
+9.8k CodeIgniter : Load different view for mobile devices
+33.3k Golang : All update packages with go get command